package com.mamesoft.medicipazienti.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class PazienteWelcomeServlet extends HttpServlet {

    private static Logger log = Logger.getLogger(ConfermaRichiamiServlet.class);

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        PropertyConfigurator.configure(PazienteWelcomeServlet.class.getClassLoader().getResource("com/mamesoft/medicipazienti/tools/log4j.properties"));

        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String nome, cognome, dataNascita, urlfoto;

        HttpSession session = request.getSession();
        nome = (String) session.getAttribute("nome");
        cognome = (String) session.getAttribute("cognome");
        dataNascita = (String) session.getAttribute("data");
        urlfoto = (String) session.getAttribute("foto");

        try {
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("    <head>");
            out.println("        <title>Vaccino Manger</title>");
            out.println("        <link rel=\"stylesheet\" type=\"text/css\" href=\"/MediciPazienti/variant-stream.css\" title=\"Variant Stream\" media=\"screen,projection\" />");
            out.println("        <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">");
            out.println("    </head>");
            out.println("    <body>");
            out.println("        <div id =\"wrap\">");
            out.println("            <div id=\"header\">");
            out.println("                <div id =\"topmenu\">");
            out.println("                    <p>");
            out.println("                        <a href=\"/MediciPazienti/logout\">Logout</a>");
            out.println("                    </p>");
            out.println("                </div>");
            out.println("                <div id=\"title\">");
            out.println("                    <h1>Vaccino Manager</h1><br/>");
            out.println("                    <h2 class =\"slogan\">I tuoi vaccini contano</h2>");
            out.println("                </div>");
            out.println("            </div>");
            out.println("            <div id=\"sidebar\">");

            out.println("   <ul class = \"sidemenu\">");
            out.println("       <li><a class=\"active\" href=\"/MediciPazienti/paziente/welcome\">Home</a><li>");
            out.println("       <li><a href=\"/MediciPazienti/paziente/visualizza_storico\">Visualizza storico</a><li>");
            out.println("       <li><a href=\"/MediciPazienti/logout\">Logout </a><li>");
            out.println("   </ul>");

            out.println("            </div>");
            out.println("            <div id =\"content\">");
            out.println("                <div id=\"feature\">");
            out.println("                    <p>" + session.getAttribute("last_login") + "</p>");
            out.println("                </div>");
            out.println("                <div class=\"post\">");

            out.println("<p>I tuoi dati:</p>");
            out.println("<table border=\"0\">");
            out.println("<tr>");
            out.println("  <td rowspan=\"3\"><img src=\"/MediciPazienti" + urlfoto.replace("jpg", "JPG") + "\"  alt=\"Foto di " + nome + " " + cognome + "\"/></td>");
            out.println("  <td>Nome:</td>");
            out.println("  <td>" + nome.toUpperCase() + "</td>");
            out.println("</tr>");
            out.println("<tr>");
            out.println("  <td>Cognome:</td>");
            out.println("  <td>" + cognome.toUpperCase() + "</td>");
            out.println("");
            out.println("</tr>");
            out.println("<tr>");
            out.println("  <td>Data di nascita:</td>");
            out.println("  <td>" + dataNascita + "</td>");
            out.println("");
            out.println("</tr>");
            out.println("</table>");
            out.println("<p>Clicca a lato per visualizzare le vaccinazioni fatte.</p>");

            out.println("                </div>");
            out.println("            </div>");
            out.println("            <div id=\"footer\">");
            out.println("                <p class=\"centered\">Copyright &copy; 2011 <a href=\"/MediciPazienti/index.html\">Mamesoft</a>");
            out.println("                    Progetto Programmazione per il web A.A.2011/2012</p>");
            out.println("            </div>");
            out.println("        </div>");
            out.println("    </body>");
            out.println("</html>");

        } catch (Exception ex) {
            request.getSession().invalidate();
            log.error("Generic error: " + ex.toString());
            response.sendRedirect("/MediciPazienti/general_error.html");
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
